<template>
  <div style="width: 100%">
    <div style="padding-bottom: 10px; padding-top: 10px">
      <el-button
        style="background-color: blanchedalmond"
        icon="el-icon-s-grid"
        size="mini"
        @click="queryQuotaCancellationHistory"
      >
        查询额度注销历史记录
      </el-button>
    </div>

    <el-table
      :data="data"
      style="width: 100%"
      @expand-change="handleExpandChange"
      row-key="contraNbr"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="100" />
      <el-table-column prop="channel" label="产品代码" />
      <el-table-column prop="productName" label="产品名称" />
      <el-table-column prop="isCancel" label="是否可注销">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isCancel == 'S'" type="success"
            >可注销</el-tag
          >
          <el-tag v-else type="danger">不可注销</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="noCancelMsg" label="不可注销原因" width="200" />

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.isCancel == 'S'"
            type="danger"
            size="mini"
            @click="handleCancel(scope.$index, scope.row)"
          >
            注销
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="cancelResult" label="注销结果">
        <template slot-scope="scope">
          <span v-if="scope.row.isCancel == 'S'" type="success">可注销</span>
          <span v-else type="danger">不可注销</span>
        </template>
      </el-table-column>

      <el-table-column type="expand" label="借据信息">
        <template slot-scope="props">
          <span style="text-align: center; display: block; margin-bottom: 1rem"
            >借据信息</span
          >
          <el-table :data="props.row.productContrDetailList" border>
            <el-table-column prop="dueBillNo" label="借据号" />
            <el-table-column prop="contrNbr" label="合同号" />
            <el-table-column prop="name" label="姓名" />
            <el-table-column prop="idNo" label="身份证号" />
            <el-table-column prop="virAcctNo" label="线下还款账户" />
            <el-table-column prop="loanProdGroup" label="贷款产品组" />
            <el-table-column prop="loanAmt" label="放款金额" />
            <el-table-column prop="applyDate" label="放款日期" />
            <el-table-column prop="totalTerms" label="放款期数" />
            <el-table-column prop="stmtDay" label="下个还款日" width="120" />
          </el-table>
        </template>
      </el-table-column>
    </el-table>

    <!-- 额度注销历史记录表格 -->

    <div style="padding-top: 60px">
      <span
        v-if="show_history"
        style="text-align: center; display: block; margin-bottom: 1rem"
        >注销历史信息</span
      >
      <el-table
        v-if="show_history"
        :data="historyData"
        style="width: 100%"
        @expand-change="handleExpandChange"
        row-key="contraNbr"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="idNo" label="证件号码"></el-table-column>

        <el-table-column prop="productCd" label="产品代码"></el-table-column>

        <el-table-column prop="cancelTime" label="注销时间"></el-table-column>

        <el-table-column prop="cancelResult" label="注销结果">
          <!-- <template slot-scope="scope">
            <span v-if="scope.row.cancelResult == 'S'" type="success"
              >成功</span
            >
            <span v-else type="danger">失败</span>
          </template> -->
        </el-table-column>

        <!-- <el-table-column prop="submitUser" label="提交人"></el-table-column> -->

        <el-table-column prop="faildMsg" label="失败原因"></el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import {
  queryCustomerLaonData,
  queryProductCreditLimitRecord,
  execProductCreditLimitCancel,
  queryCustomerLoadProductCreditLimitList,
  queryProductCreditLimitCanCancel,
} from "@/api/interface";

export default {
  data() {
    return {
      idNo: "",
      data: [],
      historyData: [],
      selectedRow: [], //选中的借据信息
      show_history: false,
      allData: {},
      // CancelHistorytab: [],
    };
  },

  created() {
    //获取身份证号
    //this.idNo = JSON.parse(sessionStorage.getItem('personalInfoData')).idNo
    //产品代码等字段按照此方法获取
    this.allData = JSON.parse(sessionStorage.getItem("personalInfoData"));
    // console.log("客户信息==", this.allData);
    //调用查询借据信息方法
    this.queryInfo();
  },

  watch: {
    $route: function (to, from) {
      console.log("监听【线下还款模块】route变化");
      this.$destroy();
    },
  },

  methods: {
    //查询借据信息(根据产品汇总)    //查询产品列表
    queryInfo() {
      //调用核心接口查询借据信息
      // console.log("查询借据信息");
      console.log("初始化查询产品列表");

      // this.data = [
      //   {
      //     appNo: "GD202408141400020786",
      //     channel: "THX00111",
      //     isProprietary: "0",
      //     subTerminalType: "",
      //     contrNbr: "HT2024081400011931",
      //     approveType: "V",
      //     isSingleApprove: "0",
      //     isCallStatus: "1",
      //     repayMethod: "MCEI",
      //     risCode: "A",
      //     cstLmtPeriod: "2199-12-31",
      //     productName: "特欢喜",
      //     associatedAppNo: "GD202407311300020684",
      //     createTime: "2024-08-14 14:11:39",
      //     orignContrNbr: "HT2024081400011931",
      //     isCancel: "E",
      //     noCancelMsg: "未结清",
      //   },
      // ];
      console.log("客户信息", this.allData.contrDetailList);

      var params = {
        mobileNo: this.allData.mobileNo,
        idNo: this.allData.idNo,
      };
      queryCustomerLoadProductCreditLimitList(params)
        .then((res) => {
          console.log("额度注销返回=", res);
          if (res.data.code == "0000") {
            this.data = res.data.data.productGroups; //新接口返回产品列表
            // var allData = this.allData.contrDetailList; //客户信息的产品列表
            // this.data.forEach((element) => {
            //   element.loanData = [
            //     {
            //       ...element,
            //     },
            //   ];
            // });
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    //查询该账户所有产品的额度注销历史记录
    queryQuotaCancellationHistory() {
      this.queryCancelHistory();
      //调接口，取核心数据
    },

    //查询选中的产品是否可注销
    // isCancelable() {
    //   console.log("选中产品", this.selectedRow);
    //   if (this.selectedRow.length == 1) {
    //     //调用核心接口并根据返回结果显示是否可注销，赋值该行的canCancel,并展示该行的注销按钮

    //     var params = {
    //       idNo: this.selectedRow.idNo, //证件号码
    //       productCd: this.selectedRow.channel, //产品代码
    //     };
    //     queryProductCreditLimitCanCancel(params)
    //       .then((res) => {
    //         console.log("选中产品是否能够注销==)", res);
    //         if (res.data.data.status == true) {
    //         }
    //       })
    //       .catch((error) => {
    //         console.log(error);
    //       });
    //     // param.idNo = this.idNo;
    //   } else if (this.selectedRow.length > 1) {
    //     window.globalUiKit.notify("提示", "最多只能选择一个产品", "error");
    //     return;
    //   } else {
    //     window.globalUiKit.notify("提示", "未选择产品信息", "error");
    //   }
    // },
    //注销
    handleCancel(index, row) {
      console.log("注销点击的数据", row);
      console.log("缓存的客户信息", this.allData.idNo);

      if (this.$store.getters.GET_OFFLINESTATUS) {
        return window.globalUiKit.notify("提示", "离线状态不能注销！", "error");
      }

      //调用核心接口获取注销结果

      var params = {
        idNo: this.allData.idNo, //证件号码
        productCd: row.channel, //产品代码
        applySeq: this.generateTimestampWithRandom(),
      };

      execProductCreditLimitCancel(params)
        .then((res) => {
          console.log("注销点击返回==)", res);
          if (res.data.data.cancelResult == "S") {
            window.globalUiKit.notify("提示", "注销成功", "success");
            this.queryInfo(); //注销之后重新查询
          } else {
            window.globalUiKit.notify("提示", res.data.data.faildMsg, "error");
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    handleExpandChange(row, expanded) {
      // 可以在这里处理展开或折叠的事件
      console.log("展开", row, expanded);
    },
    // 当选择项发生变化时，赋值选中行数据selectedRow
    handleSelectionChange(val) {
      // if (val.length > 1) {
      //   window.globalUiKit.notify("提示", "最多只能选择一个产品", "error");
      //   return;
      // }
      this.selectedRow = val;
      console.log("select选择项", this.selectedRow);
    },

    //流水，时间戳
    generateTimestampWithRandom() {
      const timestamp = Date.now(); // 获取当前时间的时间戳
      const randomNum = Math.floor(10000 + Math.random() * 90000); // 生成一个五位数的随机数
      return `${timestamp}${randomNum}`;
    },

    //注销历史记录
    queryCancelHistory() {
      if (this.selectedRow.length < 1) {
        window.globalUiKit.notify("提示", "未选择产品信息", "error");
        return;
      } else if (this.selectedRow.length > 1) {
        window.globalUiKit.notify("提示", "最多只能选择一个产品", "error");
        return;
      }

      //this.selectedRow
      console.log("查询额度注销历史记录");
      console.log(this.selectedRow);

      var params = {
        idNo: this.selectedRow[0].idNo, //证件号码
        productCd: this.selectedRow[0].channel, //产品代码
        applySeq: this.generateTimestampWithRandom(),
      };
      console.log("额度注销历史入参==", params);

      // this.historyData = [];
      // this.show_history = true;
      // var data = [
      //   {
      //     idNo: "110101197508160056",
      //     productCd: "S36001",
      //     cancelResult: "失败",
      //     faildMsg: "已销户",
      //   },
      //   {
      //     idNo: "110101197508160056",
      //     productCd: "S36001",
      //     cancelResult: "失败",
      //     submitUser: "dendw",
      //     faildMsg: "已销户",
      //   },
      //   {
      //     idNo: "110101197508160056",
      //     productCd: "S36001",
      //     cancelResult: "失败",
      //     submitUser: "dendw",
      //     faildMsg: "已销户",
      //   },
      //   {
      //     idNo: "110101197508160056",
      //     productCd: "S36001",
      //     cancelResult: "失败",
      //     faildMsg: "已销户",
      //   },
      //   {
      //     idNo: "110101197508160056",
      //     productCd: "S36001",
      //     cancelTime: "20241111171225",
      //     cancelResult: "成功",
      //     submitUser: "dendw",
      //   },
      //   {
      //     idNo: "110101197508160056",
      //     productCd: "S36001",
      //     cancelResult: "失败",
      //     faildMsg: "已销户",
      //   },
      //   {
      //     idNo: "110101197508160056",
      //     productCd: "S36001",
      //     cancelResult: "失败",
      //     faildMsg: "已销户",
      //   },
      //   {
      //     idNo: "110101197508160056",
      //     productCd: "S36001",
      //     cancelResult: "失败",
      //     faildMsg: "已销户",
      //   },
      // ];

      // data.forEach((element) => {
      //   if (element.cancelResult == "S" || element.cancelResult == "成功") {
      //     this.historyData.push(element);
      //   }
      // });
      // console.log("注销历史记录==", this.historyData);

      queryProductCreditLimitRecord(params)
        .then((res) => {
          this.historyData = [];

          this.show_history = true;
          var data = res.data.data.cancelRecord;
          if (data.length == 0) {
            return window.globalUiKit.notify("提示", "无注销历史记录", "error");
          }

          data.forEach((element) => {
            if (element.cancelResult == "S" || element.cancelResult == "成功") {
              this.historyData.push(element);
            }
          });

          console.log("注销历史记录最终展示==", this.historyData);

          // if (
          //   res.data.data.cancelResult == "S" ||
          //   res.data.data.cancelResult == "成功"
          // ) {
          //   this.historyData = res.data.data.cancelRecord;
          //   this.show_history = true;
          // } else {
          //   window.globalUiKit.notify("提示", res.data.data.faildMsg, "error");
          // }
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>